<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        
        <meta name="author" content="隐星魂 (Roy.Sun)">
        
        <link rel="shortcut icon" href="../../img/favicon.ico">
        <title>内存管理 - 玄武操作系统</title>
        <link href="../../css/bootstrap.min.css" rel="stylesheet">
        <link href="../../css/font-awesome.min.css" rel="stylesheet">
        <link href="../../css/base.css" rel="stylesheet">
        <link rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.3/styles/color-brewer.min.css">

        <script src="../../js/jquery-1.10.2.min.js" defer></script>
        <script src="../../js/bootstrap.min.js" defer></script>
        <script src="https://cdn.staticfile.org/highlight.js/10.0.3/highlight.min.js"></script>
        <script>hljs.initHighlightingOnLoad();</script> 
    </head>

    <body>
        <div class="navbar fixed-top navbar-expand-lg navbar-dark bg-primary">
            <div class="container">
                <a class="navbar-brand" href="../..">玄武操作系统</a>
                <!-- Expander button -->
                <button type="button" class="navbar-toggler" data-toggle="collapse" data-target="#navbar-collapse">
                    <span class="navbar-toggler-icon"></span>
                </button>

                <!-- Expanded navigation -->
                <div id="navbar-collapse" class="navbar-collapse collapse">
                        <!-- Main navigation -->
                        <ul class="nav navbar-nav">
                            <li class="navitem">
                                <a href="../.." class="nav-link">介绍</a>
                            </li>
                            <li class="navitem">
                                <a href="../../license/" class="nav-link">开源协议</a>
                            </li>
                            <li class="dropdown active">
                                <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown">应用手册 <b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    
<li>
    <a href="../01.%E7%AE%80%E4%BB%8B/" class="dropdown-item">简介</a>
</li>
                                    
<li>
    <a href="../02.%E7%B3%BB%E7%BB%9F%E5%9F%BA%E6%9C%AC%E6%9E%B6%E6%9E%84/" class="dropdown-item">系统基本架构</a>
</li>
                                    
<li>
    <a href="../03.%E6%9E%84%E5%BB%BA%E7%B3%BB%E7%BB%9F/" class="dropdown-item">构建系统</a>
</li>
                                    
<li>
    <a href="../04.%E5%90%AF%E5%8A%A8%E6%B5%81%E7%A8%8B/" class="dropdown-item">启动流程</a>
</li>
                                    
<li>
    <a href="../05.%E5%9F%BA%E6%9C%AC%E7%BA%A6%E5%AE%9A/" class="dropdown-item">基本约定</a>
</li>
                                    
<li>
    <a href="../06.%E4%B8%AD%E6%96%AD%E6%8E%A7%E5%88%B6/" class="dropdown-item">中断控制</a>
</li>
                                    
<li>
    <a href="../07.%E8%B0%83%E5%BA%A6%E5%99%A8/" class="dropdown-item">调度器</a>
</li>
                                    
<li>
    <a href="../08.%E7%BA%BF%E7%A8%8B/" class="dropdown-item">线程</a>
</li>
                                    
<li>
    <a href="../09.%E7%94%B5%E6%BA%90%E7%AE%A1%E7%90%86/" class="dropdown-item">电源管理</a>
</li>
                                    
<li>
    <a href="../10.%E8%BD%AF%E4%BB%B6%E5%AE%9A%E6%97%B6%E5%99%A8/" class="dropdown-item">软件定时器</a>
</li>
                                    
<li>
    <a href="../11.%E9%94%81/" class="dropdown-item">锁</a>
</li>
                                    
<li>
    <a href="../12.%E5%90%8C%E6%AD%A5/" class="dropdown-item">同步</a>
</li>
                                    
<li>
    <a href="./" class="dropdown-item active">内存管理</a>
</li>
                                    
<li>
    <a href="../14.%E7%8E%84%E6%AD%A6C%E5%BA%93/" class="dropdown-item">玄武C库</a>
</li>
                                </ul>
                            </li>
                            <li class="dropdown">
                                <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown">快速开发指南 <b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    
  <li class="dropdown-submenu">
    <a href="#" class="dropdown-item">S32k</a>
    <ul class="dropdown-menu">
            
<li>
    <a href="../../%E5%BF%AB%E9%80%9F%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97/s32k/1.XuanWuOS-envsetup-guide/" class="dropdown-item">开发环境搭建指南</a>
</li>
            
<li>
    <a href="../../%E5%BF%AB%E9%80%9F%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97/s32k/2.XuanWuOS-project-setup-guide/" class="dropdown-item">工程配置指南</a>
</li>
    </ul>
  </li>
                                    
  <li class="dropdown-submenu">
    <a href="#" class="dropdown-item">Stm32</a>
    <ul class="dropdown-menu">
            
<li>
    <a href="../../%E5%BF%AB%E9%80%9F%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97/stm32/1.XuanWuOS-envsetup-guide/" class="dropdown-item">开发环境搭建指南</a>
</li>
            
<li>
    <a href="../../%E5%BF%AB%E9%80%9F%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97/stm32/2.XuanWuOS-project-setup-guide/" class="dropdown-item">工程配置指南</a>
</li>
            
<li>
    <a href="../../%E5%BF%AB%E9%80%9F%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97/stm32/3.XuanWuOS-xwbd-atkh743/" class="dropdown-item">正点原子阿波罗H743开发板上手指南</a>
</li>
            
<li>
    <a href="../../%E5%BF%AB%E9%80%9F%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97/stm32/4.XuanWuOS-xwbd-fk429m/" class="dropdown-item">反客F429核心板上手指南</a>
</li>
            
<li>
    <a href="../../%E5%BF%AB%E9%80%9F%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97/stm32/5.XuanWuOS-xwbd-atkf407core/" class="dropdown-item">正点原子F407核心板上手指南</a>
</li>
            
<li>
    <a href="../../%E5%BF%AB%E9%80%9F%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97/stm32/6.XuanWuOS-xwbd-atkf103core/" class="dropdown-item">正点原子F103核心板上手指南</a>
</li>
    </ul>
  </li>
                                    
  <li class="dropdown-submenu">
    <a href="#" class="dropdown-item">Xwko</a>
    <ul class="dropdown-menu">
            
<li>
    <a href="../../%E5%BF%AB%E9%80%9F%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97/xwko/1.XuanWuKO-guide/" class="dropdown-item">玄武内核模块快速上手指南</a>
</li>
    </ul>
  </li>
                                </ul>
                            </li>
                            <li class="dropdown">
                                <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown">规范 <b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    
<li>
    <a href="../../%E8%A7%84%E8%8C%83/git-commit-specification/" class="dropdown-item">git commit规范</a>
</li>
                                    
<li>
    <a href="../../%E8%A7%84%E8%8C%83/version-branch-specification/" class="dropdown-item">分支与版本号</a>
</li>
                                </ul>
                            </li>
                        </ul>

                    <ul class="nav navbar-nav ml-auto">
                        <li class="nav-item">
                            <a href="#" class="nav-link" data-toggle="modal" data-target="#mkdocs_search_modal">
                                <i class="fa fa-search"></i> 搜索
                            </a>
                        </li>
                            <li class="nav-item">
                                <a rel="prev" href="../12.%E5%90%8C%E6%AD%A5/" class="nav-link">
                                    <i class="fa fa-arrow-left"></i> 上一篇
                                </a>
                            </li>
                            <li class="nav-item">
                                <a rel="next" href="../14.%E7%8E%84%E6%AD%A6C%E5%BA%93/" class="nav-link">
                                    下一篇 <i class="fa fa-arrow-right"></i>
                                </a>
                            </li>
                    </ul>
                </div>
            </div>
        </div>

        <div class="container">
            <div class="row">
                    <div class="col-md-3"><div class="navbar-light navbar-expand-md bs-sidebar hidden-print affix" role="complementary">
    <div class="navbar-header">
        <button type="button" class="navbar-toggler collapsed" data-toggle="collapse" data-target="#toc-collapse" title="Table of Contents">
            <span class="fa fa-angle-down"></span>
        </button>
    </div>

    
    <div id="toc-collapse" class="navbar-collapse collapse card bg-secondary">
        <ul class="nav flex-column">
            
            <li class="nav-item" data-level="1"><a href="#_1" class="nav-link">内存管理</a>
              <ul class="nav flex-column">
            <li class="nav-item" data-level="2"><a href="#_2" class="nav-link">简单内存分配器</a>
              <ul class="nav flex-column">
              </ul>
            </li>
            <li class="nav-item" data-level="2"><a href="#_4" class="nav-link">内存切片分配器</a>
              <ul class="nav flex-column">
              </ul>
            </li>
            <li class="nav-item" data-level="2"><a href="#_7" class="nav-link">伙伴算法分配器</a>
              <ul class="nav flex-column">
              </ul>
            </li>
            <li class="nav-item" data-level="2"><a href="#_10" class="nav-link">内存池</a>
              <ul class="nav flex-column">
              </ul>
            </li>
              </ul>
            </li>
        </ul>
    </div>
</div></div>
                    <div class="col-md-9" role="main">

<h1 id="_1">内存管理</h1>
<h2 id="_2">简单内存分配器</h2>
<h3 id="_3">描述</h3>
<p>简单内存分配器只分配大小最适应内存块，不对内存进行回收。</p>
<ul>
<li>优点：<ul>
<li>简单，代码量非常小；</li>
<li>运行时间稳定。</li>
</ul>
</li>
<li>缺点：内存一旦分配出去不再回收，<strong>free()</strong>只是一个<strong>dummy</strong>函数；</li>
<li>适用性：简单的RTOS应用场合或不需要回收内存的场合，此内存管理算法只是为了
    满足玄武OS的API而提供的默认配置；</li>
<li>上下文的安全性：在任何上下文（中断、中断底半部、线程）都是安全的。</li>
</ul>
<h3 id="api">API参考</h3>
<ul>
<li>头文件：<strong>xwos/mm/sma.h</strong></li>
<li>注释：见头文件</li>
</ul>
<h2 id="_4">内存切片分配器</h2>
<h3 id="_5">描述</h3>
<p>内存切片分配器将内存切割成等量的块，就像卡片一样，然后将它们连接在一起形成队列，
每次分配时取一块内存，回收时再把内存切片放回到队列中。</p>
<ul>
<li>优点：<ul>
<li>简单，代码量较小；</li>
<li>支持释放操作；</li>
<li>申请与释放操作的运行时间是稳定的；</li>
<li>反复申请与释放不会造成内存碎片。</li>
</ul>
</li>
<li>缺点：<ul>
<li>内存块大小固定，如果要求的内存过小，会造成内存浪费；如果要求的内存
    超过“切片”的大小，无法满足需求；</li>
<li>连续申请的两块“切片”不能保证是连续的。</li>
</ul>
</li>
<li>适用性：对时间稳定性要求高的RTOS应用场合。</li>
<li>上下文的安全性：在任何上下文（中断、中断底半部、线程）都是安全的。</li>
</ul>
<h3 id="_6">配置</h3>
<pre><code class="C">/* &lt;cfg/xwos.h&gt; */

#define XWMMCFG_MEMSLICE              1 // 是否启用内存切片分配器，取值：1|0
</code></pre>

<h3 id="api_1">API参考</h3>
<ul>
<li>头文件：<strong>xwos/mm/memslice.h</strong></li>
<li>注释：见头文件</li>
</ul>
<h2 id="_7">伙伴算法分配器</h2>
<h3 id="_8">描述</h3>
<p>伙伴算法分配器在分配时，会将内存不断地二等分，直到切割到能满足要求的最小内存大小为止。
释放时会检查与之相邻并等长的内存块（称为伙伴）是否也空闲，
如果是，就和伙伴合并成两倍大小的内存块，
然后继续检测合并后的内存块是否也存在可以合并的伙伴，一直向上合并到不能合并为止。</p>
<ul>
<li>优点：<ul>
<li>支持释放操作；</li>
<li>支持大小不固定的内存申请操作；</li>
<li>反复申请与释放不会造成内存碎片。</li>
</ul>
</li>
<li>缺点：<ul>
<li>代码有些复杂；</li>
<li>因为存在合并与切割的循环，申请与释放操作的所需要的时间不是特别稳定；</li>
<li>内存大小固定为2的指数，如果申请的内存过小，会造成内存浪费。</li>
</ul>
</li>
<li>适用性：对内存复用性要求高的应用场合。</li>
<li>上下文的安全性：在任何上下文（中断、中断底半部、线程）都是安全的。</li>
</ul>
<h3 id="_9">配置</h3>
<pre><code class="C">/* &lt;cfg/xwos.h&gt; */

#define XWMMCFG_BMA                   1 // 是否启用伙伴算法分配器，取值：1|0
</code></pre>

<h3 id="api_2">API参考</h3>
<ul>
<li>头文件：<strong>xwos/mm/bma.h</strong></li>
<li>注释：见头文件</li>
</ul>
<h2 id="_10">内存池</h2>
<h3 id="_11">描述</h3>
<p>内存池是结合了内存切片分配器与伙伴算法分配器的算法。先规定内存的基本单位为页，
一页内存为4096字节，页内存使用伙伴算法分配器管理。
内存池还使用内存切片分配器建立各种小尺寸的块：8字节、16字节、32字节、
64字节、96字节、128字节、192字节、256字节、384字节、512字节、768字节、
1024字节、2048字节。当申请内存大于2048字节，直接分配页内存；
当申请的内存小于等于2048字节，就从尺寸最合适的内存切片分配器中分配一块内存。</p>
<ul>
<li>优点：<ul>
<li>支持释放操作；</li>
<li>支持大小不固定的内存申请操作；</li>
<li>反复申请与释放不会造成内存碎片；</li>
<li>小内存块不会浪费过多的内存。</li>
</ul>
</li>
<li>缺点：<ul>
<li>代码比较复杂；</li>
<li>申请与释放操作的所需要的时间不稳定。</li>
</ul>
</li>
<li>适用性：外接尺寸较大的SRAM或SDRAM的内存管理，C++和Lua虚拟机的对象池。</li>
<li>上下文的安全性：在任何上下文（中断、中断底半部、线程）都是安全的。</li>
</ul>
<h3 id="_12">配置</h3>
<pre><code class="C">/* &lt;cfg/xwos.h&gt; */

#define XWMMCFG_MEMPOOL               1 // 是否启用内存池分配器，取值：1|0
</code></pre>

<h3 id="api_3">API参考</h3>
<ul>
<li>头文件：<strong>xwos/mm/mempool.h</strong></li>
<li>注释：见头文件</li>
</ul></div>
            </div>
        </div>

        <footer class="col-md-12">
            <hr>
            <p>Documentation built with <a href="https://www.mkdocs.org/">MkDocs</a>.</p>
        </footer>
        <script>
            var base_url = "../..",
                shortcuts = {"help": 191, "next": 78, "previous": 80, "search": 83};
        </script>
        <script src="../../js/base.js" defer></script>
        <script src="../../search/main.js" defer></script>

        <div class="modal" id="mkdocs_search_modal" tabindex="-1" role="dialog" aria-labelledby="searchModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="searchModalLabel">搜索</h4>
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
            </div>
            <div class="modal-body">
                <p>
                    在这里所有文档：
                </p>
                <form>
                    <div class="form-group">
                        <input type="search" class="form-control" placeholder="搜索..." id="mkdocs-search-query" title="Type search term here">
                    </div>
                </form>
                <div id="mkdocs-search-results"></div>
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div><div class="modal" id="mkdocs_keyboard_modal" tabindex="-1" role="dialog" aria-labelledby="keyboardModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="keyboardModalLabel">Keyboard Shortcuts</h4>
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
            </div>
            <div class="modal-body">
              <table class="table">
                <thead>
                  <tr>
                    <th style="width: 20%;">Keys</th>
                    <th>Action</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td class="help shortcut"><kbd>?</kbd></td>
                    <td>Open this help</td>
                  </tr>
                  <tr>
                    <td class="next shortcut"><kbd>n</kbd></td>
                    <td>Next page</td>
                  </tr>
                  <tr>
                    <td class="prev shortcut"><kbd>p</kbd></td>
                    <td>Previous page</td>
                  </tr>
                  <tr>
                    <td class="search shortcut"><kbd>s</kbd></td>
                    <td>Search</td>
                  </tr>
                </tbody>
              </table>
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div>

    </body>
</html>
